$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'role/list',
        datatype: "json",
        colModel: [
            {label: '角色ID', name: 'id', index: "role_id", width: 40, key: true},
            {label: '角色名称', name: 'name', index: "name", width: 75},
            {label: '显示顺序', name: 'orderNum', index: "order_num", width: 50},
            {
                label: '状态', name: 'enable', width: 60, formatter: function (value, options, row) {
                    return value === false ?
                        '<span class="label label-danger">禁用</span>' :
                        '<span class="label label-success">正常</span>';
                }
            },
            {label: '备注', name: 'remark', width: 100},
            {label: '创建人', name: 'createname', index: "create_by_id", width: 80},
            {label: '创建时间', name: 'createTime', index: "create_time", width: 80},
            {label: '修改人', name: 'updatename', index: "update_by_id", width: 80},
            {label: '修改时间', name: 'updateTime', index: "update_time"},
        ],
        viewrecords: true,
        height: 425,
        rowNum: 10,
        rowList: [10, 20, 30, 40, 50, 60, 100],
        rownumbers: true,
        rownumWidth: 30,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "data.page.list",
            page: "data.page.currPage",
            total: "data.page.totalPage",
            records: "data.page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

//菜单树
var menu_ztree;
var menu_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    check: {
        enable: true,
        nocheckInherit: true
    }
};

//部门树
var data_ztree;
var data_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    check: {
        enable: true,
        nocheckInherit: true
        // chkboxType:{ "Y" : "", "N" : "" }
    }
};


var vm = new Vue({
    el: '#pmpapp',
    data: {
        q: {
            roleName: null
        },
        showList: true,
        title: null,

        role: {
            id: null,
            name: null,
            orderNum: null,
            enable: true,
            remark: null,
            menuIdList: [],
            deptIdList: []
        }
    },
    methods: {
        //查询
        query: function () {
            vm.reload();
        },

        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'search': vm.q.name},
                page: page
            }).trigger("reloadGrid");
        },
        //重置
        reset: function () {
            window.location.href = baseURL + "modules/sys/role"
        },
        //增加
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.role = {
                id:null,
                name: null,
                orderNum: null,
                enable: true,
                remark: null,
                menuIdList: null,
                deptIdList: null
            };

            vm.getMenuTree(null);
            vm.getDataTree();
        },
        //修改
        update: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }

            vm.showList = false;
            vm.title = "修改";
            vm.getDataTree();
            vm.getMenuTree(id);

        },
        //删除
        del: function () {
            var roleIds = getSelectedRows();
            if (roleIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "role/delete",
                    contentType: "application/json",
                    data: JSON.stringify(roleIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        //回显,查询该角色已经拥有的权限信息，并显示出来
         getRole: function (id) {
            $.get(baseURL + "role/info/" + id, function (r) {

                vm.role = r.data.role;

                //勾选角色所拥有的菜单
                var menuIds = vm.role.menuIdList;

                for (var i = 0; i < menuIds.length; i++) {
                    var node = menu_ztree.getNodeByParam("id", menuIds[i]);
                    menu_ztree.checkNode(node, true, false);
                }

                //勾选角色所拥有的部门数据权限
                var deptIds = vm.role.deptIdList;
                for (var i = 0; i < deptIds.length; i++) {
                    var node = data_ztree.getNodeByParam("id", deptIds[i]);
                    data_ztree.checkNode(node, true, false);
                }

            });
        },

        saveOrUpdate: function () {

            //获取选择的菜单
            var nodes = menu_ztree.getCheckedNodes(true);
            var menuIdList = new Array();
            for (var i = 0; i < nodes.length; i++) {
                menuIdList.push(nodes[i].id);
            }
            vm.role.menuIdList = menuIdList;

            //获取选择的部门
            var nodes = data_ztree.getCheckedNodes(true);
            var deptIdList = new Array();
            for (var i = 0; i < nodes.length; i++) {
                deptIdList.push(nodes[i].id);
            }
            vm.role.deptIdList = deptIdList;

            var url = vm.role.id == null ? "role/save" : "role/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.role),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });

        },

        //加载菜单树
        getMenuTree: function (id) {
            $.get(baseURL + "permissions/list", function (r) {
                menu_ztree = $.fn.zTree.init($("#menuTree"), menu_setting, r);
                //展开所有节点
                menu_ztree.expandAll(true);

                if (id != null) {
                    vm.getRole(id);
                }
            });
        },

        //加载部门树
        getDataTree: function (id) {
            $.get(baseURL + "department/list", function (r) {
                data_ztree = $.fn.zTree.init($("#dataTree"), data_setting, r);
                data_ztree.expandAll(true);
            });
        }

    }
});